package com.xiaoran.actiiveNIO.queue_DelaySchedule;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ActiveMQMessageProducer;
import org.apache.activemq.AsyncCallback;
import org.apache.activemq.ScheduledMessage;

import javax.jms.*;
import java.util.UUID;

public class JmsProduce {
    public static final String ACTIVEMQ_URL="tcp://192.168.171.128:61616";
    public static final String QUEUE_NAMQ="queue-Delay";
    public static void main(String args[]) throws JMSException {
        //1创建连接工场，，按照给定的url地址，采用默认的用户名和密码
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        //2通过连接工厂获得连接，获得connection并启动
          Connection connection = activeMQConnectionFactory.createConnection();
          connection.start();
          //3.创建会话session
            Session session= connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            //4.创建目的地
            Queue queue=session.createQueue(QUEUE_NAMQ);
            //5.创建消息生场者,指定目的地
           MessageProducer messageProducer=session.createProducer(queue);
            //延迟投递时间
            long delay=3*1000;
            //重复投递时间
            long period=4*1000;
            //重复投递次数
            int repeat=5;
           //6，通过使用消息的生产者发送3条信息到队列；
            for (int i=1;i<=3;i++){
                //7，创建消息
                 TextMessage textMessage=session.createTextMessage("delat msg---------"+i);
                 textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY,delay);
                textMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD,period);
                textMessage.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT,repeat);
                 messageProducer.send(textMessage);
            }
             messageProducer.close();
            session.close();
            connection.close();
            System.out.println("****消息发布到MQ完成");
    }


}
